package org.jradweb.jradtest.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidStateException;
import org.hibernate.validator.InvalidValue;
import org.jradweb.core.controller.JMultiActionController;
import org.jradweb.core.utils.BeanUtils;
import org.jradweb.jradtest.model.Book;
import org.jradweb.jradtest.service.BookService;
import org.springframework.web.servlet.ModelAndView;

/**
 * BookController class .
 * 
 * Created on Fri Mar 28 15:51:01 CST 2008
 * 
 * @author Code Generator
 * @version $Id: BookController.java 93 2008-04-10 09:36:30Z wagzhi $
 */
public class BookController extends JMultiActionController {
	private static final Log log = LogFactory.getLog(BookController.class);

	private BookService bookService;

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public BookService getBookService() {
		return this.bookService;
	}

	@Override
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		List<Book> bookList = this.bookService.getAll();
		params.put("bookList", bookList);
		return this.render(request, "index", params);
	}

	public Map<String, Object> show(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		Book book = this.getBookService().getBook(Integer.parseInt(id));
		modelMap.put("book", book);

		return modelMap;

	}
	
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
	
		Book book = new Book();
		BeanUtils.setProperties(book, request);

		//InvalidValue[] values = book.getInvalidValues(request.getLocale());
		if (book.hasErrors(request.getLocale())) {
			params.put("book", book);
			return this.render("create", params);
		}
		this.bookService.save(book);

		this.getFlash(request).put("message",
				"Book saved successfully.");
		params.put("id", book.getId());

		return this.redirect("show", params);
	}
	
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		Book book = this.getBookService().getBook(Integer.parseInt(id));
	
		this.bookService.delete(book);
		this.getFlash(request).put("message", "Book "+book.getId()+" deleted");
		
		return this.redirect("index", modelMap);
	}
	
	
	public Map<String,Object> create(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		return modelMap;
	}

	public Map<String, Object> edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		Book book = this.getBookService().getBook(Integer.parseInt(id));
		modelMap.put("book", book);
		return modelMap;
	}

	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		Book book = this.getBookService().getBook(Integer.parseInt(id));
		BeanUtils.setProperties(book, request);

		//InvalidValue[] values = book.getInvalidValues(request.getLocale());
		if (book.hasErrors(request.getLocale())) {
			params.put("book", book);
			return this.render("edit", params);
		}
		this.bookService.update(book);

		this.getFlash(request).put("message",
				"Book " + book.getId() + " saved successfully.");
		params.put("id", id);

		return this.redirect("show", params);
	}

	
}